// pages/demo5/demo5.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    id:0,
    data:[],
    answer_list:[],
    unComplete:[],
    noComplete:false,
    submit:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    wx.request({
      
      url: getApp().globalData.url+'questionPage',
      data:{
        id:options.id
      },
      header:{
        "Content-Type":"application/json"
      },
      success:function(res){
        var ls= new Array(res.data.length)
        for(var i=0;i<res.data.length;i++){
          ls[i] = 0
        }
        that.setData({
          id:options.id,
          data:res.data,
          answer_list:ls
        })
      }
      
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  
  radioChange:function(e){
    var that = this
    var ls = e.detail.value.split("_");
    var num = parseInt(ls[0])
    var value = ls[1]
    var newdata = that.data.answer_list
    newdata[num-1] = value

    that.setData({
      answer:newdata,
    })
    
    var data = that.data.answer_list
  var unComplete = []
  for(var i =0;i<data.length;i++){
    if(data[i]==0){
      unComplete.push(i+1)
      
    }
  }
  if(unComplete.length == 0){
    var noComplete = false
  }else{
    var noComplete = true
  }
  that.setData({
    unComplete:unComplete,
    noComplete:noComplete
  })
      
 },
 submit:function(){
  var that = this
  var data = that.data.answer_list
  var unComplete = []
  for(var i =0;i<data.length;i++){
    if(data[i]==0){
      unComplete.push(i+1)
      
    }
  }
  if(unComplete.length == 0){
    var noComplete = false
  }else{
    var noComplete = true
  }
  that.setData({
    unComplete:unComplete,
    noComplete:noComplete
  })


   that.setData({
     submit:true
   })

 },
 cancel:function(){
  var that = this
  that.setData({
    submit:false
  })
},
confirm:function(){
  var that = this
  var userinfo = wx.getStorageSync('userinfo')

  wx.request({
    url: getApp().globalData.url+'submit/',
    data:{
      id:that.data.id,
      openid:userinfo.openid,
      answer:that.data.answer_list
    },
    success:function(res){
      wx.reLaunch({
        url: '/pages/completed/completed',
      })
    }
  })

  
}



})